$(function(){
	$('#jstree_demo').jstree({
		  "core" : {
		    "animation" : 0,
		    "check_callback" : true,
		    "themes" : { "stripes" : true },
		    'data' : {
		      'url' : "resources/xml/xtest.xml",
		      'data' : function (node) {
		        return { 'id' : node.id };
		      }
		    }
		  },
		  "types" : {
		    "#" : {
		      "max_children" : 1, 
		      "max_depth" : 4, 
		      "valid_children" : ["root"]
		    },
		    "root" : {
		      "icon" : "/static/3.1.1/assets/images/tree_icon.png",
		      "valid_children" : ["default"]
		    },
		    "default" : {
		      "valid_children" : ["default","file"]
		    },
		    "file" : {
		      "icon" : "glyphicon glyphicon-file",
		      "valid_children" : []
		    }
		  },
		"search" : {"ajax" : {
				"url" : "resources/xml/xtest.xml",
				"data" : function (str) {
					return { 
						"operation" : "search", 
						"search_str" : str };  }}},
		  "plugins" : [
"themes","json_data","ui","crrm","cookies","dnd","search","types","hotkeys","contextmenu"
		  ]
		})
	.bind("create.jstree", function (e, data) {
		$.post(
			"resources/xml/xtest.xml", 
			{ 
				"operation" : "create_node", 
				"id" : data.rslt.parent.attr("id").replace("node_",""), 
				"position" : data.rslt.position,
				"title" : data.rslt.name,
				"type" : data.rslt.obj.attr("rel")
			}, 
			function (r) {
				if(r.status) {
					$(data.rslt.obj).attr("id", "node_" + r.id);
				}
				else {
					$.jstree.rollback(data.rlbk);
				}
			}
		);
	})
	.bind("remove.jstree", function (e, data) {
		data.rslt.obj.each(function () {
			$.ajax({
				async : false,
				type: 'POST',
				url: "resources/xml/xtest.xml",
				data : { 
					"operation" : "remove_node", 
					"id" : this.id.replace("node_","")
				}, 
				success : function (r) {
					if(!r.status) {
						data.inst.refresh();
					}
				}
			});
		});
	})
	.bind("rename.jstree", function (e, data) {
		$.post(
			"resources/xml/xtest.xml", 
			{ 
				"operation" : "rename_node", 
				"id" : data.rslt.obj.attr("id").replace("node_",""),
				"title" : data.rslt.new_name
			}, 
			function (r) {
				if(!r.status) {
					$.jstree.rollback(data.rlbk);
				}
			}
		);
	})
	.bind("move_node.jstree", function (e, data) {
		data.rslt.o.each(function (i) {
			$.ajax({
				async : false,
				type: 'POST',
				url: "resources/xml/xtest.xml",
				data : { 
					"operation" : "move_node", 
					"id" : $(this).attr("id").replace("node_",""), 
					"ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""), 
					"position" : data.rslt.cp + i,
					"title" : data.rslt.name,
					"copy" : data.rslt.cy ? 1 : 0
				},
				success : function (r) {
					if(!r.status) {
						$.jstree.rollback(data.rlbk);
					}
					else {
						$(data.rslt.oc).attr("id", "node_" + r.id);
						if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
							data.inst.refresh(data.inst._get_parent(data.rslt.oc));
						}
					}
					$("#analyze").click();
				}
			});
		});
	});


;})







/**main function
 * 
 */

$(function(){
	test();
	test2();
	test3();
	})
/**tree test 
 * 
 * 
 */
function test3(){	
	    	$(function () { 
	    		$.ajaxSetup({cache:false});//ajax调用不使用缓存
	    		$("#vcsTree").jstree({//创建树开始
	    			  data : { 
	    				type : "json",//类型为json
	    				async : true,//动态操作
	    				opts : {
	    					method : "get", //设置以get方式向服务器提交数据
	    					url : "rest/XMLNode"//此页面返回以json标准格式的json数据串
	    				}
	    			 },//end data
	    			 ui:{
	    				 theme_name : "classic" //设置皮肤样式
	    			 },
	    			 lang:{     
					 loading : "目录加载中……"  //设置节点打开时默认加载显示默认为loading
				 },  
	    			 types :{
	    			 	"default" : {
	    			 		draggable : false //设置节点不可拖拽
	    			 	}
	    			 },
		  		 callback : {	 
				     beforedata : function(node,tree_obj){
							return {id : $(node).attr("id") || 0,rel : $(node).attr("rel")};//进行以异步传参 
					},
					onselect : function(node,tree_obj){//节点单击事件
							 var test = $(node).children("a").attr("href");//获取json串A标签中href属性值
							 $(parent.document.body).find('#CONTENT_WORK_MAIN').attr('src', test);
							 //获取点击的A标签的href属性，然后获取右边的iframe对象，然后设置iframe的location
					},
					onsearch : function(node, tree_obj) {//节点查找功能
						tree_obj.container.find(".search").removeClass("search");
						node.addClass("search");	
					}
				} //end callback	    
	    		});
	    	});
		     function searchnodes(){
		     	alert("start search");
		   	   jQuery.tree.focused().search(jQuery('#search_').val());
		     }
}
	

function test2(){
	$("#btn2").click(
			function loadXMLDoc()
			{
			url="resources/xml/xtest.xml";
			var xmlhttp;
			var txt,x,xx,i;
			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			    {
			    txt="<table border='1'><tr><th>Title</th><th>Artist</th></tr>";
			    x=xmlhttp.responseXML.documentElement.getElementsByTagName("book");
			    for (i=0;i<x.length;i++)
			      {
			      txt=txt + "<tr>";
			      xx=x[i].getElementsByTagName("title");
			        {
			        try
			          {
			          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
			          }
			        catch (er)
			          {
			          txt=txt + "<td> </td>";
			          }
			        }
			      xx=x[i].getElementsByTagName("price");
			        {
			        try
			          {
			          txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
			          }
			        catch (er)
			          {
			          txt=txt + "<td> </td>";
			          }
			        }
			      txt=txt + "</tr>";
			      }
			    txt=txt + "</table>";
			    document.getElementById('myDiv').innerHTML=txt;
			    }
			  }
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
			}

	);
}

 
function test(){
	$("#btn").click(
			function(){
				var employees = [
{ "firstName":"Bill" , "lastName":"Gates" },
{ "firstName":"George" , "lastName":"Bush" },
{ "firstName":"Thomas" , "lastName": "Carter" }
];
document.getElementById("fname").innerHTML=employees[1].firstName;


for (i=0;i<2;i++)
{   
	$("body").append("<div id=\"employees["+i+"].firstName\" class=\"ui-widget-content\" style=\"width: 150px; height: 150px; padding: 0.5em; border-width:5px\">"+employees[i].firstName+"<div>");
	$(".ui-widget-content").draggable();
	
}
			}
			


	)
	
	
}
